﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using NLBS.Entity;
using System.Globalization;

namespace NLBS.Data.Business
{
    public class ArticleData : CommonBase
    {
        public static readonly string OneArticleKey = "OneArticleKey:ID{0}";
        public static readonly String ArticleArrayKey = "ArticleCatArray{0}";
        public static IPagedCollection<Article> GetPageArticleByCatID(int startIndex, int pageSize, int catID)
        {
            //设置PageSetting
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            string key=string.Format(ArticleArrayKey,catID);          
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, key);

            //尝试从缓存读取
            IPagedCollection<Article> lists = help.GetPageFromCatche();
            //从数据库读取
            if (lists == null || lists.Count==0)
            {
                lists = DbProvider.Instance().GetCatArticleByPage(startIndex, pageSize, catID);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        public static readonly string ArticleUserKey = "ArticleYearKey:UserID{0}";
        public static IPagedCollection<Article> GetPageArticleByUserID(int startIndex, int pageSize, int userID)
        {
            //设置PageSetting
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            string key = string.Format(ArticleUserKey, userID);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, key);

            //尝试从缓存读取
            IPagedCollection<Article> lists = help.GetPageFromCatche();
            //从数据库读取
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetArticleByUserID(startIndex, pageSize, userID);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        public static readonly string ArticleYearKey = "ArticleYearKey:year{0}";
        public static IPagedCollection<Article> GetArticleByYear(int startIndex, int pageSize, int year, CacheDuration cacheDuration)
        {
            //设置PageSetting
            string key = string.Format(ArticleYearKey, year);
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, key);

            IPagedCollection<Article> lists = help.GetPageFromCatche();
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetPagedArticleByYear(startIndex, pageSize, year);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        private static readonly string ArticleMonthKey = "ArticleMonthKey:year{0}month{1}";

        public static IPagedCollection<Article> GetArticleByMonth(int startIndex, int pageSize, int year, int month, CacheDuration cacheDuration)
        {
            //设置PageSetting
            string key = string.Format(ArticleMonthKey, year, month);
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, key);

            IPagedCollection<Article> lists = help.GetPageFromCatche();
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetPagedArticleByMonth(startIndex, pageSize, year, month);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        private static readonly string ArticleDayKey = "ArticleDayKey:day{0}";
        public static IPagedCollection<Article> GetArticleByDay(int startIndex, int pageSize, DateTime day, CacheDuration cacheDuration)
        {
            //设置PageSetting
            string key = string.Format(ArticleDayKey, day.ToString());
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, key);

            IPagedCollection<Article> lists = help.GetPageFromCatche();
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetPagedArticleByDay(startIndex, pageSize, day);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        private static readonly string ArticleSelectedPageKey = "ArticleSelectedPage";

        public static IPagedCollection<Article> GetPageArticleBySelected(int startIndex, int pageSize)
        {
            //设置PageSetting
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, ArticleSelectedPageKey);

            IPagedCollection<Article> lists = help.GetPageFromCatche();
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetSelectedArticleByPage(startIndex, pageSize);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

       
        public static IPagedCollection<Article> GetPageArticle(int startIndex, int pageSize, CacheDuration cacheDuration)
        {
            //设置PageSetting
            if (pageSize <= 0) return null;
            PageSetting settings = new PageSetting(startIndex, pageSize, OneArticleKey);
            DefaultArticlePageHelp help = new DefaultArticlePageHelp(settings, "DefautltArticlePageKey");
            
            IPagedCollection<Article> lists = help.GetPageFromCatche();
            if (lists == null || lists.Count == 0)
            {
                lists = DbProvider.Instance().GetAllArticleByPage(startIndex, pageSize);
                if (lists != null)
                {
                    help.SetCache(lists);
                }
            }
            return lists;
        }

        public static int InsertArticle(Article item)
        {
            int artID = DbProvider.Instance().InsertArticle(item);
            item.ArticleID = artID;
            new ArticleFacade(item);
            return artID;
        }

        public static int UpdateArticle(Article item)
        {
            DbProvider.Instance().UpdateArticle(item);
            new ArticleFacade(item.ArticleID);
            return 1;
        }

        public static void UpdateArticleView(int articleID)
        {
            Article item = GetArticleByID(articleID);
            DbProvider.Instance().UpdateArticleViewCount(articleID);
            item.ViewCount++;
        }

        public static void DeleteArticle(int articleID)
        {
            DbProvider.Instance().DeleteArticle(articleID);
            new ArticleFacade(articleID);
        }

        public static IList<Article> GetTopArticle()
        {
          return GetTopArticle(BlogInfo.Instance().RecentArticleList);
        }

        public static IList<Article> GetTopArticle(int topCount)
        {
           string key = "topArticle";
           ContentCache cache = ContentCache.Instantiate();
           IList<Article> list = (List<Article>)cache[key];
           if (list == null)
           {
               list =DbProvider.Instance().GetTopArticle(topCount);
               if (list != null)
               {
                   cache.Insert(key, list, CacheDuration.Long);
               }
           }
           return list;
        }

        public static Article GetArticleByID(int ID)
        {
            string key = string.Format(OneArticleKey, ID);
            ContentCache cache = ContentCache.Instantiate();
            Article art = (Article)cache[key];
            if (art == null)
            {
                art = DbProvider.Instance().GetArticleByID(ID);
                if (art != null)
                {
                    cache.Insert(key, art, CacheDuration.Long);
                }
            }
            return art;

        }

        public Article GetArticleByID1(int ID)
        {
            string key = string.Format(OneArticleKey, ID);
            ContentCache cache = ContentCache.Instantiate();
            Article art = (Article)cache[key];
            if (art == null)
            {
                art = DbProvider.Instance().GetArticleByID(ID);
                if (art != null)
                {
                    cache.Insert(key, art, CacheDuration.Long);
                }
            }
            return art;

        }
    }
}
